/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.server.utils;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

/**
 *
 * @author Administrator OCR和以图搜图需要更新相应的字段
 */
public class OracleConnectionUtil {

    private static DataSource dataSource = null;
    static Logger logger = Logger.getLogger(OracleConnectionUtil.class.getName());

    static {
        try {
            String DBUSER = "";
            String DBPASSWD = "";
            String DBURL = "";
            String DBDRIVER = "";
            int DBSIZE = 0;
            Properties propget = new Properties();
            try {
                String relativelyPath = System.getProperty("user.dir");
                String filePath = relativelyPath + "/conf/cts_env.properties";
                FileInputStream in = new FileInputStream(filePath);
                propget.load(in);
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //�������ļ��ж�ȡ
            // DBUSER = "sysdba";
            DBUSER = propget.getProperty("DBUSER");
            // DBPASSWD = "szoscar55";
            DBPASSWD = propget.getProperty("DBPASSWD");
            // DBURL = "jdbc:oscar://localhost:2003/osrdb";
            DBURL = propget.getProperty("DBURL");
            // DBDRIVER = "com.oscar.Driver";
            DBDRIVER = propget.getProperty("DBDRIVER");
            DBSIZE = new Integer(propget.getProperty("DBSIZE"));
            Properties prop = new Properties();
//			prop.load(JdbcPool_dbcp.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
//			
//			dataSource=BasicDataSourceFactory.createDataSource(prop);
            prop.setProperty("driverClassName", DBDRIVER);
            prop.setProperty("url", DBURL);
            prop.setProperty("username", DBUSER);
            prop.setProperty("password", DBPASSWD);
            prop.setProperty("initialSize", DBSIZE + "");
            prop.setProperty("maxActive", "500");
            prop.setProperty("minIdle", "100");
            prop.setProperty("maxWait", "60000");
            prop.setProperty("connectionProperties", "useUnicode=true;characterEncoding=utf8");
            prop.setProperty("defaultAutoCommit", "true");
            prop.setProperty("defaultTransactionIsolation", "READ_COMMITTED");

            dataSource = BasicDataSourceFactory.createDataSource(prop);
            System.out.println("init oracle properties sucess");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("start");
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            String sql = "update t_search_work set createtime = ? where opid = ?";
            connection = OracleConnectionUtil.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setTimestamp(1, new Timestamp(1493913600));
            ps.setLong(2, 13344030096306443L);
            int exe = ps.executeUpdate();
            System.out.println(exe);
        } catch (SQLException ex) {
            Logger.getLogger(OracleConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            releaseConn(connection, null, null);
        }

    }

    public static Connection getConnection() {
        try {
            Connection conn = dataSource.getConnection();
            while (conn == null) {
                conn = dataSource.getConnection();
            }
            return conn;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void releaseConn(Connection connection, PreparedStatement statement,
            ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resultSet = null;
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement = null;
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            connection = null;
        }
    }

    public static void closeConn(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            connection = null;
        }
    }
}
